$(document).ready(function() {
    checklistset_fileupload();
    addchecklist_set_fileupload();
    get_hash();
	$("#submitChecklist").click(function(){
		saveChecklist();
	});
	$("#add_checklist_form").click(function(){
		addChecklistForm();
	});
	$("#addsubmitChecklist").click(function(){
		addChecklist();
	});
	
});
function checklist(){
	var asort = 0;
	checklist_icons = {};
	xfunc = "checklist/get_list_checklist";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intOrder).each(function(k,v){
					asort = result.intOrder[k];
					checklist_icons_panel = '';
						checklist_icons_panel +='<tr>';;
					    	checklist_icons_panel +='<td width="100"><img width="86" class="img-rounded" height="66" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink[k]+'" alt=""/></td>';
							checklist_icons_panel +='<td>'+result.strTitle[k]+'</td>';
							checklist_icons_panel +='<td class="controls"><div class="btn-group">';
							checklist_icons_panel +='<button class="btn btn-primary" onclick="javascript:editChecklist(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							checklist_icons_panel +='<button class="btn btn-danger" onclick="javascript:delChecklist(\''+result.intID[k]+'\', \''+result.strTitle[k]+'\')"><i class="icon-trash"></i>Delete</button>';
							checklist_icons_panel +='</td>';
							checklist_icons_panel +='</tr>';
						checklist_icons[result.intOrder[k]]=checklist_icons_panel;
				});
				
				checklist_html_panel = '<table class="table table-striped table-bordered" id="checklist_table">';
				checklist_html_panel += '<thead>';
					checklist_html_panel += '<tr>';
						checklist_html_panel += '<th class="thumb-photo">Thumbnail</th>';
						checklist_html_panel += '<th>Title</th>';
						checklist_html_panel += '<th>Control</th>';
					checklist_html_panel += '</tr>';
				checklist_html_panel += '</thead>';
				checklist_html_panel += '<tbody>';
				for(x=0;x<=asort;x++){
					if($.trim(checklist_icons[x])!=""){
						checklist_html_panel +=checklist_icons[x];
					}
								
				}		
				checklist_html_panel += '</tbody>';		
				checklist_html_panel += '</table>';
				
				$('#checklist_content').html(checklist_html_panel);
				$('#checklist_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addChecklistForm(){
	$("#checklist_content, #add_checklist_form").hide();
	$("#addChecklist").slideDown('slow', function(){
		$("#checklist_content").hide();
		$("#addChecklist").slideDown('slow', function(){
			$(".back-btn-addchecklist a").attr("onclick","btn_back_addchecklist()");
		});	
	});	
}
function addChecklist(){
	var strNoteTitle = $("#addcheckliststrTitle").val();
	var strImgLink = $("#addchecklist_thumbnailLink").val();
	if(strNoteTitle==''){
		$("#errorDiary").html("<div class='alert alert-error'>Error: Put the title!</div>");
	} else if(strImgLink=='') {
		$("#errorDiary").html("<div class='alert alert-error'>Error: Put the image thumbnail!</div>");
	} else {
		xfunc = "checklist/save_checklist";
		data_sent = {
				'strTitle':strNoteTitle,
				'strImgLink':strImgLink
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorChecklist").hide();
				$("#addchecklistSuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#addcheckliststrTitle").val('');
				$("#addchecklist_thumbnailLink").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editChecklist(noteID){
	xfunc = "checklist/get_checklist";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			var checklist_thumbnail = '<div class="imgThumbnail"><img width="86" class="img-rounded" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink+'" alt=""/></div>';
			$("#intChecklistID").val(result.intID);
			$("#checkliststrTitle").val(result.strTitle);
			$("#checklist_thumbnail").html(checklist_thumbnail);
			$("#checklist_thumbnailLink").val(result.strImgLink);
		}
	});

	$("#add_checklist_form").hide();
	$("#checklist_content").hide();
	$("#editChecklist").slideDown('slow', function(){
		$(".back-btn-checklist a").attr("onclick","btn_back_checklist()");
	});	
}
function saveChecklist(){
	var intID = $("#intChecklistID").val();
	var strTitle = $("#checkliststrTitle").val();
	var strImgLink = $("#checklist_thumbnailLink").val();
	
	xfunc = "checklist/save_checklist";
	data_sent = {
			'intID':intID,
			'strTitle':strTitle,
			'strImgLink':strImgLink
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#checklistSuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delChecklist(noteID, titleName){
	xfunc = "checklist/delete_checklist";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				checklist();
			}
		});
    });
}
function btn_back_addchecklist(){
	$("#addChecklist").hide();
	$("#checklist_content").slideDown('slow', function(){
		$("#add_checklist_form").show();
		checklist();
	});	
}
function btn_back_checklist(){
	$("#editChecklist").hide();
	$("#checklist_content").slideDown('slow', function(){
		$("#add_checklist_form").show();
		checklist();
	});	
}
function checklistset_fileupload(){
	$('#checklist_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	checklistfile_upload();
}
function checklistfile_upload(){
	$('.checklistfileupload').fileupload();
	$('.checklistfileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#checklist_thumbnailLink").val(v.name);			
					$(".imgThumbnail").hide();
					$(".imgThumbnail").slideDown('slow', function(){
						$(".imgThumbnail").html(html);
					});					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}

//ADD DATA
function addchecklist_set_fileupload(){
	$('#addchecklist_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	addchecklist_file_upload();
}
function addchecklist_file_upload(){
	$('.addchecklistfileupload').fileupload();
	$('.addchecklistfileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#addchecklist_thumbnailLink").val(v.name);			
					$("#addchecklist_thumbnail").html(html);			
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}